﻿using UnityEngine;
using System.Collections;
using System;

public class PvpManager : MonoBehaviour {

	private static PvpManager instance;
	
	public static PvpManager Instance{
		get{
			return instance;
		}
	}	
	
	void Awake()
	{
		instance = this;
		playerPosParent = GameObject.Find("PCPos").transform.FindChild("1");
		enemyPosParent = GameObject.Find("NPCPos").transform.FindChild("1");
		
		GameObject uiExist = GameObject.Find("UI Root");
		if(uiExist!=null)
			uiExist.SetActive(false);
		
//		if( GameObject.Find("ViewerUI") == null)
//		{
//			GameObject viewerUI = Instantiate( Resources.Load ("Prefab/Battle/ViewerUI") as GameObject ) as GameObject;
//		}
		//		OriginalCamTrans = new Transform[3];
		//		OriginalCamTrans[0] = GameObject.Find("CamPos1").transform;
		//		OriginalCamTrans[1] = GameObject.Find("CamPos2").transform;
		//		OriginalCamTrans[2] = GameObject.Find("CamPos3").transform;
		
		GameObject go = Instantiate( Resources.Load ("Prefab/Battle/BattleResManager") as GameObject ) as GameObject;
		
	}
	
	void Start()
	{
		InitCam ();
		if(playerPrefab==null)
			playerPrefab = Resources.Load ("Prefab/Battle/Character/Assassin_M_BrietraLS_A1") as GameObject;
		if(enemyPrefab==null)
			enemyPrefab = Resources.Load ("Prefab/Battle/Character/Haskin_M_C1") as GameObject;
		if(playerPrefab!=null)
			CreatePlayer();
		if(enemyPrefab!=null)
			CreateEnemy();
		playerPrefabOld = playerPrefab;
		enemyPrefabOld = enemyPrefab;
		
		distBetween =  (playerTrans.position - enemyTrans.position).magnitude;
		initialDistBetween = distBetween;
		Debug.Log("distBetween  "+distBetween);
	}
	
	
	public EasyJoystick easyJoystick;
	
	public float distBetween;
	float initialDistBetween;
	Transform playerPosParent;
	Transform enemyPosParent;
	public GameObject playerPrefab;
	GameObject playerPrefabOld;
	public GameObject enemyPrefab;
	GameObject enemyPrefabOld;
	[HideInInspector]
	
	public float lookAtOffsetY = 0.7f;
	public GameObject playerGo;
	public GameObject enemyGo;
	Transform playerTrans;
	Transform enemyTrans;

	PvpPawn player;
	PvpPawn enemy;
	[HideInInspector]
	public GameObject cameraRoot;
	[HideInInspector]
	public PvpCamera touchCam;
	[HideInInspector]
	public GameObject middleCam;
	
	public Transform[] OriginalCamTrans = new Transform[3];
	[HideInInspector]
	public float OriginalCamFOV;
	
	public bool turnOffSkillCam;
	public bool turnOffCamShake;
	public bool repeatPlay;
	
	[HideInInspector]
	public float curTimeScale = 1;
	
	void InitCam()
	{
		if(cameraRoot==null) {
			OriginalCamFOV = Camera.main.fieldOfView;
			cameraRoot = Camera.main.gameObject.transform.parent.parent.gameObject;
			touchCam = cameraRoot.GetComponent<PvpCamera>();
			if(touchCam==null) {
				touchCam = cameraRoot.AddComponent<PvpCamera>();
				touchCam.Reset();
			}
			middleCam = cameraRoot.transform.FindChild("Middle").gameObject;
			
			EventlineStaticActor.Camera = cameraRoot;
			if(middleCam!=null)
				EventlineStaticActor.MiddleCam = middleCam;
			EventlineStaticActor.MainCamera = Camera.main.gameObject;

			if(cameraRoot.GetComponent<PvpCameraMouse>() == null)
				cameraRoot.AddComponent<PvpCameraMouse>().Reset();
			
			GameObject originalCamPosGo = new GameObject();
			originalCamPosGo.name="originalCamPosGo";
			originalCamPosGo.transform.position = cameraRoot.transform.position;
			originalCamPosGo.transform.rotation = cameraRoot.transform.rotation;
			OriginalCamTrans[0] = originalCamPosGo.transform;
		}
	}
	
	void CreatePlayer()
	{
		//if(playerGo!=null)
		//	Destroy(playerGo);
		//playerGo = Instantiate(playerPrefab) as GameObject;
		playerGo.name = playerPrefab.name;
		playerGo.transform.parent = playerPosParent;
		playerGo.transform.localPosition = Vector3.zero;
		playerGo.transform.localRotation = Quaternion.identity;
		playerGo.transform.localScale = Vector3.one;
		playerGo.transform.parent = null;
		playerGo.GetComponent<Animation>().playAutomatically=false;
		player = playerGo.AddComponent<PvpPawn>();
		player.Init(true);
		
		playerTrans = playerGo.transform;
		easyJoystick.XAxisTransform = playerTrans;
		easyJoystick.XTI = EasyJoystick.PropertiesInfluenced.Translate;
		easyJoystick.xAI = EasyJoystick.AxisInfluenced.X;
		easyJoystick.inverseXAxis=true;
		easyJoystick.YAxisTransform = playerTrans;
		easyJoystick.YTI = EasyJoystick.PropertiesInfluenced.Translate;
		easyJoystick.yAI = EasyJoystick.AxisInfluenced.Z;
		easyJoystick.inverseYAxis=true;
	}
	void CreateEnemy()
	{
		//if(enemyGo!=null)
		//	Destroy(enemyGo);
		//enemyGo = Instantiate(enemyPrefab) as GameObject;
		enemyGo.name = enemyPrefab.name;
		enemyGo.transform.parent = enemyPosParent;
		enemyGo.transform.localPosition = Vector3.zero;
		enemyGo.transform.localRotation = Quaternion.identity;
		enemyGo.transform.localScale = Vector3.one;
		enemyGo.transform.parent = null;
		enemyGo.GetComponent<Animation>().playAutomatically=false;
		enemy = enemyGo.AddComponent<PvpPawn>();
		enemy.Init(false);
		
		enemyTrans = enemyGo.transform;
	}
	
	
	public void OnAction(int nType)
	{
		if(nType>=1 && nType<=3)
			ResetCamera(nType);
		else if(nType==0)
		{}//PlayTake();
		else if(nType==10)
			ToggleTimeScale();
	}
	
	void ToggleTimeScale()
	{
		if(curTimeScale==1)
			curTimeScale=0.5f;
		else if(curTimeScale==0.5)
			curTimeScale=0.25f;
		else if(curTimeScale==0.25)
			curTimeScale=1;
		Time.timeScale=curTimeScale;
	}
	
	public void ResetCamera(int nType)
	{
		//CamTakeSetting.Instance.SetBackTakeCamWaitTime(0);
		touchCam.DeactivateFreeCam();
		BackToOriginalCamPos(nType);
	}
	
	public void BackToOriginalCamPos(int nType)
	{
		Debug.Log("BackToOriginalCamPos  curfov = "+Camera.main.fieldOfView+"   curTimeScale="+Time.timeScale);
		Camera.main.fieldOfView = OriginalCamFOV;
		Time.timeScale=1.0f;
		if(OriginalCamTrans!=null && nType-1 < OriginalCamTrans.Length && OriginalCamTrans[nType-1] !=null) {
			cameraRoot.transform.position = OriginalCamTrans[nType-1].position;
			cameraRoot.transform.rotation = OriginalCamTrans[nType-1].rotation;
		}
	}
	
	Vector3 newLookAt;
	
	void Update()
	{
		if(playerPrefabOld!=playerPrefab) {
			playerPrefabOld = playerPrefab;
			CreatePlayer();
		}
		if(enemyPrefabOld!=enemyPrefab) {
			enemyPrefabOld = enemyPrefab;
			CreateEnemy();
		}
		
		if(turnOffSkillCam) {
			EventlineStaticActor.Camera=null;
		}
		else {
			if(EventlineStaticActor.Camera==null)
				EventlineStaticActor.Camera=cameraRoot;
		}
		
		if(turnOffCamShake) {
			EventlineStaticActor.MiddleCam=null;
		}
		else {
			if(EventlineStaticActor.MiddleCam==null)
				EventlineStaticActor.MiddleCam = middleCam;
		}
		
		newLookAt = (playerTrans.position + enemyTrans.position)*0.5f;
		newLookAt.y += lookAtOffsetY;
		touchCam.LookAt = newLookAt;
		
		distBetween =  (playerTrans.position - enemyTrans.position).magnitude;
		touchCam.SetDistance(distBetween);
	}
}
